﻿using community.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace community.Controllers
{
    [Route("api/event-registrations")]
    [ApiController]
    public class EventRegistrationsController : ControllerBase
    {
        private static List<EventRegistration> registrations = new List<EventRegistration>();

        // 活动报名
        [HttpPost]
        public IActionResult Register([FromBody] EventRegistration registration)
        {
            var existingRegistration = registrations.Find(r => r.EventId == registration.EventId && r.UserId == registration.UserId);
            if (existingRegistration != null)
            {
                return BadRequest("User already registered for this event.");
            }

            registration.Id = registrations.Count + 1;
            registration.RegisteredAt = DateTime.UtcNow;
            registrations.Add(registration);
            return CreatedAtAction(nameof(GetRegistrationsByEvent), new { eventId = registration.EventId }, registration);
        }

        // 查询活动的所有报名信息
        [HttpGet("event/{eventId}")]
        public IActionResult GetRegistrationsByEvent(int eventId)
        {
            var eventRegistrations = registrations.FindAll(r => r.EventId == eventId);
            return Ok(eventRegistrations);
        }

        // 查询用户的所有报名信息
        [HttpGet("user/{userId}")]
        public IActionResult GetRegistrationsByUser(int userId)
        {
            var userRegistrations = registrations.FindAll(r => r.UserId == userId);
            return Ok(userRegistrations);
        }

        // 取消报名
        [HttpDelete("{id}")]
        public IActionResult CancelRegistration(int id)
        {
            var registration = registrations.Find(r => r.Id == id);
            if (registration == null)
            {
                return NotFound();
            }

            registrations.Remove(registration);
            return NoContent();
        }
    }
}
